package org.adg.code.controller;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.adg.code.model.ReportUser;
import org.adg.code.service.UserManager;
import org.adg.code.service.UserManagerImpl;

public class UserController {

	/*To check whether report user name exists*/
	public boolean userNameExists(String un)
	{
		UserManager um=new UserManagerImpl();
		return um.userNameExists(un);
	}
	
	/*To save the new user details */
	/*Gets the values entered by the user from the jsp and passes it to the manager*/
	public boolean saveUser(HttpServletRequest request, HttpServletResponse response)
	{
		
			HttpSession session = request.getSession(); 
			ReportUser rs=new ReportUser();
			rs.setusername(request.getParameter("username"));
			rs.setuname((String)session.getAttribute("uname"));
			rs.setfname(request.getParameter("fname"));
			rs.setlname(request.getParameter("lname"));
			rs.setadd1(request.getParameter("add1"));
			rs.setadd2(request.getParameter("add2"));
			rs.setadd3(request.getParameter("add3"));
			rs.setphno(request.getParameter("phno"));
			rs.setmob(request.getParameter("mob"));
			rs.setusertitle(request.getParameter("usertitle"));
			rs.setedu(request.getParameter("edu"));
			rs.setlicense(request.getParameter("license"));
			rs.setvalidupto(request.getParameter("validupto"));
 			 		
 			UserManager um=new UserManagerImpl();
 			return um.saveUser(rs);
 			
 		
 		
	}
	
	/*To get the list of Report users who are under the logged in user*/
	/*Passes the name of the user logged in to the manager*/
	public ResultSet getUserList(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession();
		UserManager um=new UserManagerImpl();
		String un = (String)session.getAttribute("uname");
		ResultSet rs=um.getReportUserList(un);
		return rs;
		
	}
	
	/*To get the details of the report user selected by the user to edit*/
	/*gets the selected user name from the jsp and passes it to the manager*/
	public ResultSet getUserDetails(HttpServletRequest request, HttpServletResponse response)
	{
		 
				 UserManager um=new UserManagerImpl();
				 String uname = request.getParameter("name");
	 			ResultSet rs=um.getReportUserDetails(uname);
	 			return rs;
	}
	
	/*To store the updated report user details*/
	/*gets the new values entered by the user from jsp and sends it to the manager*/
	public boolean userupdate(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(); 
		
			ReportUser rs=new ReportUser();
			rs.setid(request.getParameter("id"));
			rs.setusername(request.getParameter("username"));
			rs.setuname((String)session.getAttribute("uname"));
			rs.setfname(request.getParameter("fname"));
			rs.setlname(request.getParameter("lname"));
			rs.setadd1(request.getParameter("add1"));
			rs.setadd2(request.getParameter("add2"));
			rs.setadd3(request.getParameter("add3"));
			rs.setphno(request.getParameter("phno"));
			rs.setmob(request.getParameter("mob"));
			rs.setusertitle(request.getParameter("usertitle"));
			rs.setedu(request.getParameter("edu"));
			rs.setlicense(request.getParameter("license"));
			rs.setvalidupto(request.getParameter("validupto"));
			UserManager um=new UserManagerImpl();
			return um.ReportUserUpdate(rs);
			
			
	}
	
	/*To delete the selected report user*/
	/*gets the selected report user name from the jsp and passes to the manager*/
	public boolean removeUser(HttpServletRequest request, HttpServletResponse response)
	{
		
			String uname = request.getParameter("name");
			UserManager um=new UserManagerImpl();
			return um.deleteReportUser(uname);
			
		 	
	}
}
